package com.example.springboot_manage.controller;

import com.example.springboot_manage.model.*;
import com.example.springboot_manage.repository.*;
import com.example.springboot_manage.util.ResultUtil;
import com.example.springboot_manage.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.AccessibleObject;
import java.math.BigDecimal;

import java.util.List;

@RestController
@RequestMapping("/merchant")
public class MerchantHandler {

    @Autowired
    private ShoeInfoRepository shoeInfoRepository;

    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private SpecsInfoRepository specsInfoRepository;

    @Autowired
    private SpecsColorRepository specsColorRepository;

    @Autowired
    private SpecsSizeRepository specsSizeRepository;

    @RequestMapping("/shoeInfo")
    public ResultVO findShoeInfo(@RequestParam("page") String page, @RequestParam("limit") String limit,
                                 HttpSession session){
        Account account = (Account) session.getAttribute("loginInfo");
        int pageSize = Integer.parseInt(limit);
        int startIndex = (Integer.parseInt(page) - 1) * pageSize;
        List<ShoeInfo> shoeInfos = shoeInfoRepository.findByCategoryType(startIndex,pageSize,account.getCategoryType());
        int count = shoeInfoRepository.queryByTypeCount(account.getCategoryType());
        return ResultUtil.success(shoeInfos,count);
    }

    @RequestMapping("/updateShoeInfo")
    public String updateInfo(@RequestParam("shoeId") String shoeId,
                             @RequestParam("shoeName") String shoeName,
                             @RequestParam("shoePrice") String shoePrice,
                             @RequestParam("shoeDescription") String shoeDescription,
                             @RequestParam("shoeStock") String shoeStock,
                             @RequestParam("shoeIcon") String shoeIcon,HttpSession session) {
        Account account = (Account) session.getAttribute("loginInfo");
        ShoeInfo shoeInfo = new ShoeInfo();
        shoeInfo.setShoeId(Integer.parseInt(shoeId));
        shoeInfo.setShoeName(shoeName);
        shoeInfo.setShoeIcon(shoeIcon);
        shoeInfo.setShoeDescription(shoeDescription);
        shoeInfo.setShoePrice(shoePrice);
        shoeInfo.setShoeStock(shoeStock);
        shoeInfo.setCategoryType(account.getCategoryType());
        System.out.println(shoeInfo);
        int flag = shoeInfoRepository.update(shoeInfo);
        if (flag > 0) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping("/delShoeInfo")
    public String delShoeInfo(@RequestParam("id") String shoeId){
        int id = Integer.parseInt(shoeId);
        int flag = shoeInfoRepository.deleteById(id);
        if (flag != 0){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/saveShoeInfo")
    public String saveCategory(@RequestParam("shoeName") String shoeName,
                               @RequestParam("shoePrice") String shoePrice,
                               @RequestParam("shoeDescription") String shoeDescription,
                               @RequestParam("shoeStock") String shoeStock,
                               @RequestParam("shoeIcon") String shoeIcon,HttpSession session){
        ShoeInfo shoeInfo = new ShoeInfo();
        shoeInfo.setShoeName(shoeName);
        shoeInfo.setShoeIcon(shoeIcon);
        shoeInfo.setShoeDescription(shoeDescription);
        shoeInfo.setShoePrice(shoePrice);
        shoeInfo.setShoeStock(shoeStock);
        Account account = (Account) session.getAttribute("loginInfo");
        shoeInfo.setCategoryType(account.getCategoryType());
        int flag = shoeInfoRepository.save(shoeInfo);
        if (flag > 0) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping("/orderMaster")
    public ResultVO findOrder(@RequestParam("page") String page, @RequestParam("limit") String limit,
                              HttpSession session){
        Account account = (Account) session.getAttribute("loginInfo");
        int pageSize = Integer.parseInt(limit);
        int startIndex = (Integer.parseInt(page) - 1) * pageSize;
        List<OrderMaster> orderMasters = orderMasterRepository.findByCategoryType(startIndex,pageSize,account.getCategoryType());
        int count = orderMasterRepository.queryByTypeCount(account.getCategoryType());
        return ResultUtil.success(orderMasters,count);
    }

    @RequestMapping("/delOrderMaster")
    public String delOrderMaster(@RequestParam("id") String orderId){
        int id = Integer.parseInt(orderId);
        int flag = orderMasterRepository.deleteById(id);
        if (flag != 0){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/updateOrderMaster")
    public String updateOrderMaster(@RequestParam("orderId") String orderId,
                                    @RequestParam("buyerName") String buyerName,
                                    @RequestParam("buyerPhone") String buyerPhone,
                                    @RequestParam("buyerAddress") String buyerAddress,
                                    @RequestParam("shoeId") String shoeId,
                                    @RequestParam("shoeName") String shoeName,
                                    @RequestParam("shoeQuantity") String shoeQuantity,
                                    @RequestParam("shoeIcon") String shoeIcon,
                                    @RequestParam("specsId") String specsId,
                                    @RequestParam("specsNameColor") String specsNameColor,
                                    @RequestParam("specsNameSize") String specsNameSize,
                                    @RequestParam("specsPrice") String specsPrice,
                                    @RequestParam("orderAmount") String orderAmount,
                                    @RequestParam("payStatus") String payStatus,HttpSession session) {
        Account account = (Account) session.getAttribute("loginInfo");
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setOrderId(orderId);
        orderMaster.setOrderAmount(new BigDecimal(orderAmount));
        orderMaster.setBuyerAddress(buyerAddress);
        orderMaster.setShoeIcon(shoeIcon);
        orderMaster.setShoeId(Integer.parseInt(shoeId));
        orderMaster.setShoeName(shoeName);
        orderMaster.setBuyerPhone(buyerPhone);
        orderMaster.setShoeQuantity(shoeQuantity);
        orderMaster.setSpecsId(Integer.parseInt(specsId));
        orderMaster.setSpecsNameColor(specsNameColor);
        orderMaster.setSpecsNameColor(specsNameColor);
        orderMaster.setSpecsNameSize(specsNameSize);
        orderMaster.setSpecsPrice(new BigDecimal(specsPrice));
        orderMaster.setPayStatus(Integer.parseInt(payStatus));
        orderMaster.setBuyerName(buyerName);
        orderMaster.setCategoryType(account.getCategoryType());
        int flag = orderMasterRepository.update(orderMaster);
        System.out.println(orderMaster);
        if (flag > 0) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping("/updateInfo")
    public String updateCategory(@RequestParam("categoryId") String categoryId,
                                 @RequestParam("categoryName") String categoryName,
                                 @RequestParam("categoryType") String categoryType,
                                 @RequestParam("introduce") String introduce) {
        int id = Integer.parseInt(categoryId);
        ShoeCategory category = new ShoeCategory();
        category.setCategoryId(id);
        category.setCategoryName(categoryName);
        category.setIntroduce(introduce);
        category.setCategoryType(Integer.parseInt(categoryType));
        int flag = categoryRepository.update(category);
        if (flag > 0) {
            return "true";
        } else {
            return "false";
        }
    }

    /*
    对规格的crud
     */
    @RequestMapping("/specsInfo")
    public ResultVO specsInfo(@RequestParam("page") String page, @RequestParam("limit") String limit,
                              HttpSession session){
        int pageSize = Integer.parseInt(limit);
        int startIndex = (Integer.parseInt(page) - 1) * pageSize;
        Account account = (Account) session.getAttribute("loginInfo");
        List<SpecsInfo> specsInfo = specsInfoRepository.findByCategoryType(startIndex,pageSize,account.getCategoryType());
        int count = specsInfoRepository.queryByTypeCount(account.getCategoryType());
        return ResultUtil.success(specsInfo,count);
    }

    @RequestMapping("/delSpecsInfo")
    public String delSpecsInfo(@RequestParam("id") String specsId){
        int id = Integer.parseInt(specsId);
        int flag = specsInfoRepository.deleteById(id);
        if (flag != 0){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/updateSpecsInfo")
    public String updateSpecsInfo(@RequestParam("specsId") String specsId,
                                  @RequestParam("specsNameSize") String specsNameSize,
                                  @RequestParam("specsNameColor") String specsNameColor,
                                  @RequestParam("shoeId") String shoeId,
                                  @RequestParam("specsStock") String specsStock,
                                  @RequestParam("specsPrice") String specsPrice,
                                  HttpSession session) {
        SpecsInfo specsInfo = new SpecsInfo();
        specsInfo.setSpecsId(Integer.parseInt(specsId));
        specsInfo.setSpecsNameSize(specsNameSize);
        specsInfo.setSpecsNameColor(specsNameColor);
        specsInfo.setShoeId(Integer.parseInt(shoeId));
        specsInfo.setSpecsPrice(specsPrice);
        specsInfo.setSpecsStock(Integer.parseInt(specsStock));
        Account account = (Account) session.getAttribute("loginInfo");
        specsInfo.setCategoryType(account.getCategoryType());
        SpecsColor specsColor = specsColorRepository.findByColorName(specsNameColor,Integer.parseInt(shoeId));
        SpecsSize specsSize = specsSizeRepository.findBySizeName(specsNameSize);
        if (specsColor != null && specsSize != null){
            specsInfo.setSpecsIdColor(specsColor.getSpecsId());
            specsInfo.setSpecsIdSize(specsSize.getSpecsId());
            int flag = specsInfoRepository.update(specsInfo);
            if (flag > 0) {
                return "true";
            } else {
                return "false";
            }
        }else {
            return "false";
        }



    }

    @RequestMapping("/saveSpecsInfo")
    public String saveSpecsInfo(@RequestParam("specsNameSize") String specsNameSize,
                                @RequestParam("specsNameColor") String specsNameColor,
                                @RequestParam("shoeId") String shoeId,
                                @RequestParam("specsStock") String specsStock,
                                @RequestParam("specsPrice") String specsPrice,
                                HttpSession session){
        SpecsInfo specsInfo = new SpecsInfo();
        specsInfo.setSpecsNameSize(specsNameSize);
        specsInfo.setSpecsNameColor(specsNameColor);
        specsInfo.setShoeId(Integer.parseInt(shoeId));
        specsInfo.setSpecsPrice(specsPrice);
        specsInfo.setSpecsStock(Integer.parseInt(specsStock));
        Account account = (Account) session.getAttribute("loginInfo");
        specsInfo.setCategoryType(account.getCategoryType());
        SpecsSize specsSize = specsSizeRepository.findBySizeName(specsNameSize);
        SpecsColor specsColor = specsColorRepository.findByColorName(specsNameColor,Integer.parseInt(shoeId));
        specsInfo.setSpecsIdColor(specsColor.getSpecsId());
        specsInfo.setSpecsIdSize(specsSize.getSpecsId());
        if (specsColor != null && specsSize != null){
            specsInfo.setSpecsIdColor(specsColor.getSpecsId());
            specsInfo.setSpecsIdSize(specsSize.getSpecsId());
            int flag = specsInfoRepository.save(specsInfo);
            if (flag > 0) {
                return "true";
            } else {
                return "false";
            }
        }else {
            return "false";
        }
    }

    /*
    对规格颜色的crud
     */
    @RequestMapping("/specsColor")
    public ResultVO specsColor(@RequestParam("page") String page, @RequestParam("limit") String limit,
                               HttpSession session){
        int pageSize = Integer.parseInt(limit);
        int startIndex = (Integer.parseInt(page) - 1) * pageSize;
        Account account = (Account) session.getAttribute("loginInfo");
        List<SpecsColor> specsColors = specsColorRepository.findByCategoryType(startIndex,pageSize,account.getCategoryType());
        int count = specsColorRepository.queryByTypeCount(account.getCategoryType());
        return ResultUtil.success(specsColors,count);
    }

    @RequestMapping("/delSpecsColor")
    public String delSpecsColor(@RequestParam("id") String specsId){
        int id = Integer.parseInt(specsId);
        int flag = specsColorRepository.deleteById(id);
        if (flag != 0){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/updateSpecsColor")
    public String updateSpecsColor(@RequestParam("specsId") String specsId,
                                   @RequestParam("specsName") String specsName,
                                   @RequestParam("shoeId") String shoeId,
                                   @RequestParam("specsIcon") String specsIcon,
                                   @RequestParam("specsPreview") String specsPreview,
                                   HttpSession session) {
        SpecsColor specsColor = new SpecsColor();
        specsColor.setSpecsId(Integer.parseInt(specsId));
        specsColor.setSpecsName(specsName);
        specsColor.setShoeId(Integer.parseInt(shoeId));
        specsColor.setSpecsIcon(specsIcon);
        specsColor.setSpecsPreview(specsPreview);
        Account account = (Account) session.getAttribute("loginInfo");
        specsColor.setCategoryType(account.getCategoryType());
        SpecsColor specsColor1 = specsColorRepository.findByColorName(specsName,Integer.parseInt(shoeId));
        if (specsColor1 == null || (specsColor1 != null && specsColor1.getSpecsId() == specsColor.getSpecsId())){
            int flag = specsColorRepository.update(specsColor);
            if (flag > 0) {
                return "true";
            } else {
                return "false";
            }
        }else{
            return "false";
        }
    }

    @RequestMapping("/saveSpecsColor")
    public String saveSpecsColor(@RequestParam("specsName") String specsName,
                                 @RequestParam("shoeId") String shoeId,
                                 @RequestParam("specsIcon") String specsIcon,
                                 @RequestParam("specsPreview") String specsPreview,
                                 HttpSession session){
        SpecsColor specsColor = new SpecsColor();
        specsColor.setSpecsName(specsName);
        specsColor.setShoeId(Integer.parseInt(shoeId));
        specsColor.setSpecsIcon(specsIcon);
        specsColor.setSpecsPreview(specsPreview);
        Account account = (Account) session.getAttribute("loginInfo");
        specsColor.setCategoryType(account.getCategoryType());
        SpecsColor specsColor1 = specsColorRepository.findByColorName(specsName,Integer.parseInt(shoeId));
        if (specsColor1 == null){
            int flag = specsColorRepository.save(specsColor);
            if (flag > 0){
                return "true";
            }else{
                return "false";
            }
        }else{
            return "false";
        }
    }

    /*
    对规格尺码的crud
     */
    @RequestMapping("/specsSize")
    public ResultVO specsSize(@RequestParam("page") String page, @RequestParam("limit") String limit){
        int pageSize = Integer.parseInt(limit);
        int startIndex = (Integer.parseInt(page) - 1) * pageSize;
        List<SpecsSize> specsSizes = specsSizeRepository.findAll(startIndex,pageSize);
        int count = specsSizeRepository.queryAllCount();
        return ResultUtil.success(specsSizes,count);
    }

    @RequestMapping("/delSpecsSize")
    public String delSpecsSize(@RequestParam("id") String specsId){
        int id = Integer.parseInt(specsId);
        int flag = specsSizeRepository.deleteById(id);
        if (flag != 0){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/updateSpecsSize")
    public String updateSpecsSize(@RequestParam("specsId") String specsId,
                                  @RequestParam("specsName") String specsName) {
        SpecsSize specsSize = new SpecsSize();
        specsSize.setSpecsId(Integer.parseInt(specsId));
        specsSize.setSpecsName(specsName);
        SpecsSize specsSize1 = specsSizeRepository.findBySizeName(specsName);
        if (specsSize1 == null || (specsSize1 != null && specsSize1.getSpecsId() == specsSize.getSpecsId())){
            int flag = specsSizeRepository.update(specsSize);
            if (flag > 0) {
                return "true";
            } else {
                return "false";
            }
        }else{
            return "false";
        }
    }

    @RequestMapping("/saveSpecsSize")
    public String saveSpecsSize(@RequestParam("specsName") String specsName){
        SpecsSize specsSize = new SpecsSize();
        specsSize.setSpecsName(specsName);
        SpecsSize specsSize1 = specsSizeRepository.findBySizeName(specsName);
        if (specsSize1 == null){
            int flag = specsSizeRepository.save(specsSize);
            if (flag > 0){
                return "true";
            }else{
                return "false";
            }
        }else{
            return "false";
        }
    }

    @Autowired
    private BulletinRepository bulletinRepository;

    @RequestMapping("/findTitles")
    public List<String> findTitles(){
        List<String> titles = bulletinRepository.findTitles();
        return titles;
    }

    //获取标题返回数据
    @RequestMapping("/findBulletin")
    public Bulletin findBulletin(@RequestParam("title")String title){
        Bulletin bulletin = bulletinRepository.findByTitle(title);
        return bulletin;
    }



}
